﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TruongVu.Data;

namespace TruongVu.Business
{
    public class CounterModel
    {
        public int GetToday()
        {
            // Today
            string today = DateTime.Today.ToString("yyyyMMdd");
            Counter todayCounter = (from c in Singleton.DataContext.Counters
                                    where c.Type == "TODAY_" + today
                                    select c).FirstOrDefault();
            if (todayCounter == null)
            {
                return 0;
            }
            else
            {
                return todayCounter.Count;
            }
        }

        public int GetMonth()
        {
            string month = DateTime.Today.ToString("yyyyMM");
            Counter monthCounter = (from c in Singleton.DataContext.Counters
                                    where c.Type == "MONTH_" + month
                                    select c).FirstOrDefault();
            if (monthCounter == null)
            {
                return 0;
            }
            else
            {
                return monthCounter.Count;
            }
        }

        public int GetTotal()
        {
            Counter totalCounter = (from c in Singleton.DataContext.Counters
                                    where c.Type == "TOTAL"
                                    select c).FirstOrDefault();
            if (totalCounter == null)
            {
                return 0;
            }
            else
            {
                return totalCounter.Count;
            }
        }

        public void IncreaseCounterBy1()
        {
            // Today
            string today = DateTime.Today.ToString("yyyyMMdd");
            Counter todayCounter = (from c in Singleton.DataContext.Counters
                                    where c.Type == "TODAY_" + today
                                    select c).FirstOrDefault();
            if (todayCounter != null)
            {
                todayCounter.Count++;
            }
            else
            {
                todayCounter = new Counter();
                todayCounter.Type = "TODAY_" + today;
                todayCounter.Count = 1;
                Singleton.DataContext.Counters.InsertOnSubmit(todayCounter);
            }

            // MONTH
            string month = DateTime.Today.ToString("yyyyMM");
            Counter monthCounter = (from c in Singleton.DataContext.Counters
                                    where c.Type == "MONTH_" + month
                                    select c).FirstOrDefault();
            if (monthCounter != null)
            {
                monthCounter.Count++;
            }
            else
            {
                monthCounter = new Counter();
                monthCounter.Type = "MONTH_" + month;
                monthCounter.Count = 1;
                Singleton.DataContext.Counters.InsertOnSubmit(monthCounter);
            }

            // TOTAL
            Counter allCounter = (from c in Singleton.DataContext.Counters
                                  where c.Type == "TOTAL"
                                  select c).FirstOrDefault();
            if (allCounter != null)
            {
                allCounter.Count++;
            }
            else
            {
                allCounter = new Counter();
                allCounter.Type = "TOTAL";
                allCounter.Count = 1;
                Singleton.DataContext.Counters.InsertOnSubmit(allCounter);
            }

            // delete other today/month
            List<Counter> deleteCounters = (from c in Singleton.DataContext.Counters
                                            where (c.Type.Contains("TODAY") && c.Type != "TODAY_" + today) ||
                                                  (c.Type.Contains("MONTH") && c.Type != "MONTH_" + month)
                                            select c).ToList();

            // submit changes
            Singleton.DataContext.SubmitChanges();
        }
    }
}
